<template>
    <el-card>
        <div slot="header">
            项目：
            <el-button type="text" @click="$router.push('/project/'+project.id)">
                {{project.name}}
            </el-button>
            <br/>
            主播：
            <el-button type="text" @click="$router.push('/anchor/'+anchor.id)">
                {{anchor.name}}
            </el-button>
        </div>
        <h4>平台：{{anchor.platform.name}} </h4>
        <hr>
        <p>直播记录</p>
        <Schedules_list mode="project_anchor" :project="project" :anchor="anchor"/>
    </el-card>
</template>

<script>
    import _ from 'lodash';

    import Project_add_anchor_schedule from "../component/project_add_anchor_schedule";
    import Schedules_list from "../component/schedules_list";

    export default {
        components: {
            Schedules_list,
            Project_add_anchor_schedule
        },
        data() {
            return {
                dialogVisible: false,
                project: {id: 0, name: ''},
                anchor: {id: 0, name: '', platform: {name: ''}},
                schedules: [],
            };
        },
        methods: {
            fetch() {
                const {id: project_id, anchor_id} = this.$route.params;
                Promise.all([
                    this.$http.get('/api/project/' + project_id).then(res => {
                        this.project = res.data;
                    }),
                    this.$http.get('/api/xiaohulu/anchor/' + anchor_id).then(res => {
                        this.anchor = res.data;
                    })])
                    .catch(() => {
                        this.$router.replace('/404');
                    });
            },
            addSchedule(schedule) {
                schedule.time = [schedule.start, schedule.end];
                this.schedules.push(schedule);
            },
            deleteSchedule(schedule) {
                this.$confirm(`确认删除这个时间安排吗？`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    const {id: project_id, anchor_id} = this.$route.params;
                    this.$http.delete(`/api/project/${project_id}/anchor/${anchor_id}/schedule/${schedule.id}`);
                    this.schedules.splice(this.schedules.indexOf(schedule), 1);
                    this.$message.success('删除成功!');
                }).catch(() => {
                });
            },
            submit(row) {
                if (_.isEmpty(row.start) || _.isEmpty(row.end) || !(row.loop > 0))
                    return;
                const {id: project_id, anchor_id} = this.$route.params;
                this.$http.post(`/api/project/${project_id}/anchor/${anchor_id}/schedule`, row).then(res => {
                    if (!row.hasOwnProperty('id'))
                        row.id = res.data.id;
                    this.$message.success('记录修改成功');
                }).catch(err => {
                    this.$message.error(err.response.data);
                });
            }
        },
        computed: {},
        mounted() {
            this.fetch();
        },
    }
</script>

<style scoped>

</style>